#include "hzpch.h"
#include "OpenGLRendererAPI.h"
#include "Renderer/OrthographicCamera.h"
#include "Debug/Instrumentor.h"

namespace Hazel{
    void OpenGLMessageCallback(
            unsigned source,
            unsigned type,
            unsigned id,
            unsigned severity,
            int length,
            const char* message,
            const void* userParam)
    {
        switch (severity)
        {
            case GL_DEBUG_SEVERITY_HIGH:         HZ_CORE_CRITICAL(message); return;
            case GL_DEBUG_SEVERITY_MEDIUM:       HZ_CORE_ERROR(message); return;
            case GL_DEBUG_SEVERITY_LOW:          HZ_CORE_WARN(message); return;
            case GL_DEBUG_SEVERITY_NOTIFICATION: HZ_CORE_TRACE(message); return;
        }

        HZ_CORE_ASSERT(false, "Unknown severity level!");
    }
    void OpenGLRendererAPI::SetClearColor(const glm::vec4 &color) {
        glClearColor(color.r, color.g, color.b, color.a);
    }

    void OpenGLRendererAPI::Clear() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }

    void OpenGLRendererAPI::DrawIndexed(const Ref<VertexArray> &vertexArray) {
        glDrawElements(GL_TRIANGLES, vertexArray->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, nullptr);
    }



    // 开启混合(Blend)
    void OpenGLRendererAPI::Init() {
        HZ_PROFILE_FUNCTION();
#ifdef HZ_DEBUG
        glEnable(GL_DEBUG_OUTPUT);
		glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
		glDebugMessageCallback(OpenGLMessageCallback, nullptr);

		glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, NULL, GL_FALSE);
#endif

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        glEnable(GL_DEPTH_TEST);
    }
    void OpenGLRendererAPI::SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
        // mac 屏幕上有问题，不知道为啥 glViewport设置窗口宽高和位置
        glViewport(x, y, width, height);
    }
}
